<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼栋号" prop="gradeDormitory">
        <el-input
          v-model="queryParams.gradeDormitory"
          placeholder="请输入楼栋号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宿舍号" prop="gradeBuilding">
        <el-input
          v-model="queryParams.gradeBuilding"
          placeholder="请输入宿舍号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地面整洁" prop="gradeGround">
        <el-input
          v-model="queryParams.gradeGround"
          placeholder="请输入地面整洁"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="无滞留垃圾" prop="gradeLitter">
        <el-input
          v-model="queryParams.gradeLitter"
          placeholder="请输入无滞留垃圾"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="床上整洁" prop="gradeBed">
        <el-input
          v-model="queryParams.gradeBed"
          placeholder="请输入床上整洁"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="桌面整齐" prop="gradeDesk">
        <el-input
          v-model="queryParams.gradeDesk"
          placeholder="请输入桌面整齐"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="阳台干净" prop="gradeBalcony">
        <el-input
          v-model="queryParams.gradeBalcony"
          placeholder="请输入阳台干净"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="违规电器" prop="gradeWiring">
        <el-input
          v-model="queryParams.gradeWiring"
          placeholder="请输入违规电器"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分星级" prop="gradeRanking">
        <el-input
          v-model="queryParams.gradeRanking"
          placeholder="请输入评分星级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分人" prop="gradeScorer">
        <el-input
          v-model="queryParams.gradeScorer"
          placeholder="请输入评分人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:grade:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:grade:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:grade:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:grade:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评分ID" align="center" prop="gradeId" />
      <el-table-column label="楼栋号" align="center" prop="gradeDormitory" />
      <el-table-column label="宿舍号" align="center" prop="gradeBuilding" />
      <el-table-column label="地面整洁" align="center" prop="gradeGround" />
      <el-table-column label="无滞留垃圾" align="center" prop="gradeLitter" />
      <el-table-column label="床上整洁" align="center" prop="gradeBed" />
      <el-table-column label="桌面整齐" align="center" prop="gradeDesk" />
      <el-table-column label="阳台干净" align="center" prop="gradeBalcony" />
      <el-table-column label="违规电器" align="center" prop="gradeWiring" />
      <el-table-column label="评分星级" align="center" prop="gradeRanking" />
      <el-table-column label="评分人" align="center" prop="gradeScorer" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:grade:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:grade:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼栋号" prop="gradeDormitory">
          <el-input v-model="form.gradeDormitory" placeholder="请输入楼栋号" />
        </el-form-item>
        <el-form-item label="宿舍号" prop="gradeBuilding">
          <el-input v-model="form.gradeBuilding" placeholder="请输入宿舍号" />
        </el-form-item>
        <el-form-item label="地面整洁" prop="gradeGround">
          <el-input v-model="form.gradeGround" placeholder="请输入地面整洁" />
        </el-form-item>
        <el-form-item label="无滞留垃圾" prop="gradeLitter">
          <el-input v-model="form.gradeLitter" placeholder="请输入无滞留垃圾" />
        </el-form-item>
        <el-form-item label="床上整洁" prop="gradeBed">
          <el-input v-model="form.gradeBed" placeholder="请输入床上整洁" />
        </el-form-item>
        <el-form-item label="桌面整齐" prop="gradeDesk">
          <el-input v-model="form.gradeDesk" placeholder="请输入桌面整齐" />
        </el-form-item>
        <el-form-item label="阳台干净" prop="gradeBalcony">
          <el-input v-model="form.gradeBalcony" placeholder="请输入阳台干净" />
        </el-form-item>
        <el-form-item label="违规电器" prop="gradeWiring">
          <el-input v-model="form.gradeWiring" placeholder="请输入违规电器" />
        </el-form-item>
        <el-form-item label="评分星级" prop="gradeRanking">
          <el-input v-model="form.gradeRanking" placeholder="请输入评分星级" />
        </el-form-item>
        <el-form-item label="评分人" prop="gradeScorer">
          <el-input v-model="form.gradeScorer" placeholder="请输入评分人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrade, getGrade, delGrade, addGrade, updateGrade } from "@/api/system/grade";

export default {
  name: "Grade",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      gradeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gradeDormitory: null,
        gradeBuilding: null,
        gradeGround: null,
        gradeLitter: null,
        gradeBed: null,
        gradeDesk: null,
        gradeBalcony: null,
        gradeWiring: null,
        gradeRanking: null,
        gradeScorer: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gradeDormitory: [
          { required: true, message: "楼栋号不能为空", trigger: "blur" }
        ],
        gradeBuilding: [
          { required: true, message: "宿舍号不能为空", trigger: "blur" }
        ],
        gradeGround: [
          { required: true, message: "地面整洁不能为空", trigger: "blur" }
        ],
        gradeLitter: [
          { required: true, message: "无滞留垃圾不能为空", trigger: "blur" }
        ],
        gradeBed: [
          { required: true, message: "床上整洁不能为空", trigger: "blur" }
        ],
        gradeDesk: [
          { required: true, message: "桌面整齐不能为空", trigger: "blur" }
        ],
        gradeBalcony: [
          { required: true, message: "阳台干净不能为空", trigger: "blur" }
        ],
        gradeWiring: [
          { required: true, message: "违规电器不能为空", trigger: "blur" }
        ],
        gradeRanking: [
          { required: true, message: "评分星级不能为空", trigger: "blur" }
        ],
        gradeScorer: [
          { required: true, message: "评分人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listGrade(this.queryParams).then(response => {
        this.gradeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gradeId: null,
        gradeDormitory: null,
        gradeBuilding: null,
        gradeGround: null,
        gradeLitter: null,
        gradeBed: null,
        gradeDesk: null,
        gradeBalcony: null,
        gradeWiring: null,
        gradeRanking: null,
        gradeScorer: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gradeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gradeId = row.gradeId || this.ids
      getGrade(gradeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gradeId != null) {
            updateGrade(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrade(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gradeIds = row.gradeId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + gradeIds + '"的数据项？').then(function() {
        return delGrade(gradeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/grade/export', {
        ...this.queryParams
      }, `grade_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
